package com.gaohe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.R;
import com.gaohe.pojo.Dish;
import com.gaohe.pojo.DishDto;
import com.gaohe.service.DishFlavorService;
import com.gaohe.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    DishFlavorService dishFlavorService;


    //分页查询
    @GetMapping("/page")
    public R<Page<DishDto>> page(Integer page, Integer pageSize, String name) {
        Page<DishDto> dishDtoPage = dishService.DISH_DTO_PAGE(page, pageSize, name);
        return R.success(dishDtoPage);
    }

    //新增菜品
    //1:向dish表插入数据，并返回dish_id;
    //2:将dish_id赋值给flavor表的dish_id字段
    //3：插入dish_flavor表的数据
    @PostMapping
    public R<String> addDish(@RequestBody DishDto dishDto) {
//        log.info("dishDto:{}",dishDto);
        dishService.saveDishDto(dishDto);
        return R.success("菜品创建成功");
    }

    //修改数据的菜品回显
    @GetMapping("/{id}")
    public R<DishDto> updateDish1(@PathVariable("id") Long id) {
        DishDto dishDto = dishService.updateDishAndFlavor1(id);
        return R.success(dishDto);
    }

    //修改数据
    @PutMapping
    public R<String> updateDish2(@RequestBody DishDto dishDto) {
        dishService.updateDishAndFlavor2(dishDto);
        return R.success("修改成功");
    }

    //菜品停售
    //先查数据，再修改某一字段，再加入进行修改数据
    @PostMapping("/status/{status}")
    public R<String> stopSales(@PathVariable("status") Integer status, String ids) {
//        log.info("status:{},ids:{}",status, ids);
        dishService.stop(status, ids);
        return R.success("停售成功");
    }

    //删除数据
    @DeleteMapping
    public R<String> deleteDish(String ids) {
        dishService.deleteDishAndFlavor(ids);
        return R.success("删除数据成功");
    }

    //套餐新增的菜品回显
    @GetMapping("/list")
    public R<List<DishDto>> dishList(Dish dish) {
        List<DishDto> dishDtos = dishService.DISH_DTO_LIST(dish);
        return R.success(dishDtos);
    }

}
